// pages/Main/mainIndex/mainIndex.js
var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
var wxCharts = require('../../../utils/Component/wxChart/wxcharts.js');
var pieChart = null;
var pieChart2 = null;
var pieChart3 = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zsd: "",
    zts: "",
    dts: "",
    defenlv: "",
    content: [],
    learnarr: [],
  }, 
  touchHandler: function (e) {
    // console.log(pieChart.getCurrentDataIndex(e));
    for (var a = 0; a < this.data.content.length; a++) {
      if (pieChart.getCurrentDataIndex(e) == a) {
        this.setData({
          zsd: this.data.content[a].knowledgepoints_knowledgepoint_title,
          zts: this.data.content[a].acount,
          dts: this.data.content[a].wcount,
          defenlv: (this.data.content[a].wcount / this.data.content[a].acount * 100).toFixed(2) + "%",
        });
      }
    }
  },
  /* touchHandler: function (e) {
    // console.log(pieChart.getCurrentDataIndex(e));
    for (var a = 0; a < this.data.learnarr.length; a++) {
      if (pieChart.getCurrentDataIndex(e) == a) {
        this.setData({
          zsd: this.data.learnarr[a].learningresultid_learningresult_title,
          zts: this.data.learnarr[a].acount1,
          dts: this.data.learnarr[a].wcount1,
          defenlv: (this.data.learnarr[a].wcount1 / this.data.learnarr[a].acount1 * 100).toFixed(2) + "%",
        });
      }
    }
  },
  touchHandler2: function (e) {
    // console.log(pieChart.getCurrentDataIndex(e));
    for (var a = 0; a < this.data.habitarr.length; a++) {
      if (pieChart2.getCurrentDataIndex(e) == a) {
        this.setData({
          zsd: this.data.habitarr[a].habitid_habit_title,
          zts: this.data.habitarr[a].acounth,
          dts: this.data.habitarr[a].wcounth,
          defenlv: (this.data.habitarr[a].wcounth / this.data.habitarr[a].acounth * 100).toFixed(2) + "%",
        });
      }
    }
  },
  touchHandler3: function (e) {
    // console.log(pieChart.getCurrentDataIndex(e));
    for (var a = 0; a < this.data.interestarr.length; a++) {
      if (pieChart3.getCurrentDataIndex(e) == a) {
        this.setData({
          zsd: this.data.interestarr[a].interestid_interest_title,
          zts: this.data.interestarr[a].acountin,
          dts: this.data.interestarr[a].wcountin,
          defenlv: (this.data.interestarr[a].wcountin / this.data.interestarr[a].acountin * 100).toFixed(2) + "%",
        });
      }
    }
  }, */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let c = common.Getorgid();
    if (c.role == "学生") {
      this.setData({
        orgid: c.orgid,
        userid: c.userid,
      });
    }
    else {
      this.setData({
        orgid: c.orgid,
        userid: options.id,
      });
    }
    this.weak();
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
  },
  //8.10.1查询薄弱知识点的列表
  weak: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "peopleid", "searchValue": this.data.userid, "page": 1, "num": 1000 }
    businessLogic.QueryWeak(params, (res) => {
      if (res.length > 0) {
        console.log("8.10.1", res)
       /*  var learnarr = this.offlikelearn(res)
        var habitarr = this.offlikehabit(res)
        for (var i = 0; i < learnarr.length; i++) {
          let num = 0;
          let num2 = 0;
          for (var j = 0; j < res.length; j++) {
            if (learnarr[i].learningresultid_learningresult_title == res[j].learningresultid_learningresult_title) {
              num = num + res[j].acount;
              num2 = num2 + res[j].wcount
            }
          }
          learnarr[i].acount1 = num;
          learnarr[i].wcount1 = num2;
        }
        for (var i = 0; i < habitarr.length; i++) {
          let num = 0;
          let num2 = 0;
          for (var j = 0; j < res.length; j++) {
            if (habitarr[i].habitid_habit_title == res[j].habitid_habit_title) {
              num = num + res[j].acount;
              num2 = num2 + res[j].wcount
            }
          }
          habitarr[i].acounth = num;
          habitarr[i].wcounth = num2;
        }
        var interestarr = this.offlikeinterest(res)
        for (var i = 0; i < interestarr.length; i++) {
          let num = 0;
          let num2 = 0;
          for (var j = 0; j < res.length; j++) {
            if (interestarr[i].interestid_interest_title == res[j].interestid_interest_title) {
              num = num + res[j].acount;
              num2 = num2 + res[j].wcount
            }
          }
          interestarr[i].acountin = num;
          interestarr[i].wcountin = num2;
        }
        this.setData({
          learnarr: learnarr,
          habitarr: habitarr,
          interestarr: interestarr,
          zsd: learnarr[0].learningresultid_learningresult_title,
          zts: learnarr[0].acount1,
          dts: learnarr[0].wcount1,
          defenlv: (learnarr[0].wcount1 / learnarr[0].acount1 * 100).toFixed(2) + "%",
        })
        console.log("f", this.data.learnarr) 
        var windowWidth = 320;
        try {
          var resa = wx.getSystemInfoSync();
          windowWidth = resa.windowWidth;
        } catch (e) {
          console.error('getSystemInfoSync failed!');
        }
        var series = []
        for (var a = 0; a < this.data.learnarr.length; a++) {
          series.push({ name: this.data.learnarr[a].learningresultid_learningresult_title, data: this.data.learnarr[a].wcount1 })
        }
        pieChart = new wxCharts({
          animation: true,
          canvasId: 'pieCanvas',
          type: 'pie',
          series: series,
          width: windowWidth,
          height: 300,
          dataLabel: true,
        });
        var series2 = []
        for (var a = 0; a < this.data.habitarr.length; a++) {
          series2.push({ name: this.data.habitarr[a].habitid_habit_title, data: this.data.habitarr[a].wcounth })
        }
        pieChart2 = new wxCharts({
          animation: true,
          canvasId: 'pieCanvas2',
          type: 'pie',
          series: series2,
          width: windowWidth,
          height: 300,
          dataLabel: true,
        });
        var series3 = []
        for (var a = 0; a < this.data.interestarr.length; a++) {
          series3.push({ name: this.data.interestarr[a].interestid_interest_title, data: this.data.interestarr[a].wcountin })
        }
        pieChart3 = new wxCharts({
          animation: true,
          canvasId: 'pieCanvas3',
          type: 'pie',
          series: series3,
          width: windowWidth,
          height: 300,
          dataLabel: true,
        });*/
        this.setData({
          content:res,
          zsd: res[0].knowledgepoints_knowledgepoint_title,
          zts: res[0].acount,
          dts: res[0].wcount,
          defenlv: (res[0].wcount / res[0].acount * 100).toFixed(2) + "%",
        })
        var windowWidth = 320;
        try {
          var resa = wx.getSystemInfoSync();
          windowWidth = resa.windowWidth;
        } catch (e) {
          console.error('getSystemInfoSync failed!');
        }
        var series = []
        for (var a = 0; a < this.data.content.length; a++) {
          series.push({ name: this.data.content[a].knowledgepoints_knowledgepoint_title, data: this.data.content[a].wcount })
        }
        pieChart = new wxCharts({
          animation: true,
          canvasId: 'pieCanvas',
          type: 'pie',
          series: series,
          width: windowWidth,
          height: 300,
          dataLabel: true,
        });
      }
      else {
        this.setData({
          content: "暂无数据",
          zsd: "暂无数据",
          zts: "暂无数据",
          dts: "暂无数据",
          defenlv: "暂无数据",
        });
      }
    })
  },
  /* offlikelearn: function (array) {
    var arr = array,
      result = [],
      i,
      j,
      len = arr.length;
    for (i = 0; i < len; i++) {
      for (j = i + 1; j < len; j++) {
        if (arr[i].learningresultid_learningresult_title === arr[j].learningresultid_learningresult_title) {
          j = ++i;
        }
      }
      result.push(arr[i]);
    }
    return result;
  },
  offlikehabit: function (array) {
    var arr = array,
      result = [],
      i,
      j,
      len = arr.length;
    for (i = 0; i < len; i++) {
      for (j = i + 1; j < len; j++) {
        if (arr[i].habitid_habit_title === arr[j].habitid_habit_title) {
          j = ++i;
        }
      }
      result.push(arr[i]);
    }
    return result;
  },
  offlikeinterest: function (array) {
    var arr = array,
      result = [],
      i,
      j,
      len = arr.length;
    for (i = 0; i < len; i++) {
      for (j = i + 1; j < len; j++) {
        if (arr[i].interestid_interest_title === arr[j].interestid_interest_title) {
          j = ++i;
        }
      }
      result.push(arr[i]);
    }
    return result;
  }, */
  misTopic: function () {
    wx.navigateTo({
      url: "../misTopic/misTopic"
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})